#include "stdafx.h"
#include "CppUnitTest.h"
#include "..\CExpressionUtil\Expression.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace CExpression;
using namespace std;

namespace CExpressionUtilUnitTest
{
	TEST_CLASS(CExpressionGeneralUnitTest)
	{
	public:
		TEST_METHOD(CExpressioinGeneralDecimalTest)
		{
			Expression exp(TEXT("(10+2-1)*sum(1,2,3,4,5)/2+(-1)"));
			Assert::IsFalse(exp.ErrorOccured());
			Assert::AreEqual(static_cast<int>(ExecuteRunTimeType::Double),static_cast<int>(exp.ResultType()));
            Assert::AreEqual(81.5,exp.DoubleResult(),0.0001);
		}

		TEST_METHOD(CExpressioinVariableTest)
		{
			Expression exp(TEXT("(a+b-1)*sum(1,2,3,4,5)/2"));
			Assert::IsFalse(exp.ErrorOccured());
            vector<const ExpressionString*> *symbols = exp.VariableSymbols();
			ExpressionString variables[]={TEXT("a"),TEXT("b")};
			double val=0;
            for(size_t i=0;i!=symbols->size();++i,++val)
			{
				Assert::AreEqual<ExpressionString>(variables[i],*((*symbols)[i]));
				exp.SetVariableValue(*((*symbols)[i]),val+1);
			}
			Assert::AreEqual(static_cast<int>(ExecuteRunTimeType::Double),static_cast<int>(exp.ResultType()));
			Assert::AreEqual(15,exp.DoubleResult(),0.0001);
		}

		TEST_METHOD(CExpressioinNestingTest)
		{
			Expression exp(TEXT("(1+2-1)*sum(avg(1,1),2,3,4,5)/2"));
			Assert::IsFalse(exp.ErrorOccured());
			Assert::AreEqual(static_cast<int>(ExecuteRunTimeType::Double),static_cast<int>(exp.ResultType()));
            Assert::AreEqual(15,exp.DoubleResult(),0.0001);
		}

		TEST_METHOD(CExpressioinGeneralBoolTest)
		{
			Expression exp(TEXT("((1+2-1)*sum(1,2,3,4,5)/2)>=15"));
			Assert::AreEqual(static_cast<int>(ExecuteRunTimeType::Boolean),static_cast<int>(exp.ResultType()));
			Assert::IsFalse(exp.ErrorOccured());
            Assert::AreEqual(true,exp.BoolResult());
		}
	};
}